//
//  OrderDetailViewController.m
//  UEAssistant
//
//  Created by mac on 2017/1/23.
//  Copyright © 2017年 youyixinxi. All rights reserved.
//

#import "OrderDetailViewController.h"
#import "OrderDetailTableViewCell.h"
#import "CustomAlertView.h"
#import "PreviewViewController.h"
#import "DropOrderTopCell.h"
#import "DropOrderMidCell.h"

@interface OrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>{
    
    NSArray                 *_titleArray_top;
    NSArray                 *_titleArray_mid;
    NSMutableArray          *_types;
    BOOL                    _isSuccess;
    NSMutableArray          *_cellHeight;
}

@property (nonatomic, strong) UIView                *redView;
@property (nonatomic, strong) UIView                *backView;
@property (nonatomic, strong) UITableView           *tableView;
@property (nonatomic, strong) UIButton              *button;
@property (nonatomic, strong) UILabel               *tagLabel;
@property (nonatomic, strong) UILabel               *tagLabeltwo;
@property (nonatomic, strong) UILabel               *tagLabelThr;
@property (nonatomic, strong) UILabel               *tagLabelFour;
@property (nonatomic, strong) UIImageView           *typeImageV;
@property (nonatomic, assign) BOOL                  leadTitleTraint_bool;
@property (nonatomic, strong) UIButton              *dropBt;

@end

@implementation OrderDetailViewController

- (void)viewWillAppear:(BOOL)animated{
    
}

- (void)viewWillDisappear:(BOOL)animated{
    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initlizeUserface];
    [self initlizeDataSource];
}

- (void)initlizeUserface{
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = self.docName;
    [self.view addSubview:self.tableView];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = item;
    
    _tableView.rowHeight =  UITableViewAutomaticDimension;
    _tableView.estimatedRowHeight = 40*Radio;
    
    
    [self.view addSubview:self.dropBt];
}

- (void)initlizeDataSource{
    
    _cellHeight = [NSMutableArray arrayWithObject:@"50"];
    _titleArray_top = @[@"所属项目",@"截止时间"];
    _titleArray_mid = @[@"语种",@"字数",@"任务类型",@"任务标签"];
    _types = [NSMutableArray array];
    _isSuccess = NO;
    _leadTitleTraint_bool = NO;
}


- (void)didPresentControllerButtonTouch{
    
    CustomAlertView *cusView = [[CustomAlertView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
    cusView.center = CGPointMake(self.backView.center.x,self.backView.center.y);
    cusView.layer.cornerRadius = 3;
    cusView.layer.masksToBounds = YES;
    if (_isSuccess == YES) {
    }
    else{
        [cusView.button setImage:[UIImage imageNamed:@"failure"] forState:UIControlStateNormal];
        cusView.messageLabel.text = @"~很遗憾~";
        cusView.subMessageLabel.text = @"已经被别人捷足先登了，下次记得早点来哦！";
    }
    [self.view addSubview:cusView];
}

#pragma <UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0 || indexPath.row == 1) {
        
        return 70*Radio;
    }
    else if (indexPath.row == 2){
        
        return 30*Radio + [YhxUtility autoSizeToThatWidth:Screen_Width-30 fontsize:FLEXIBLE(14) content:self.docName];
    }
    else if (indexPath.row > 2 && indexPath.row < 7){
        
        return 10*Radio + [YhxUtility autoSizeToThatWidth:Screen_Width-90*2 fontsize:FLEXIBLE(13) content:self.dataSource[indexPath.row]];
    }
    else{
        
        return [YhxUtility autoSizeToThatWidth:Screen_Width-30 fontsize:FLEXIBLE(14) content:@"任务描述"] + [YhxUtility autoSizeToThatWidth:Screen_Width-30 fontsize:FLEXIBLE(14) content:self.dataSource[indexPath.row]] + 30*Radio;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 0 || indexPath.row == 1) {
        
        DropOrderTopCell *cell = [[DropOrderTopCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellOne"];
        cell.titleLabel.text = _titleArray_top[indexPath.row];
        cell.nameLabel.text = self.dataSource[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row == 2){
        
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellTwo"];
        UILabel *docName = [[UILabel alloc]initWithFrame:CGRectMake(15, 20*Radio,Screen_Width-30, [YhxUtility autoSizeToThatWidth:Screen_Width-30 fontsize:FLEXIBLE(14) content:self.docName])];
        docName.numberOfLines = 0;
        docName.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        docName.textColor = RGB(47, 47, 47, 1);
        docName.textAlignment = NSTextAlignmentLeft;
        docName.font = [UIFont systemFontOfSize:FLEXIBLE(14)];
        docName.text = self.docName;
        [cell addSubview:docName];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if (indexPath.row > 2 && indexPath.row < 7){
        
        DropOrderMidCell *cell = [[DropOrderMidCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellThree"];
        cell.titleLabel.text = _titleArray_mid[indexPath.row - 3];
        cell.nameLabel.text = self.dataSource[indexPath.row];
        if (indexPath.row == 6) {
            [cell.nameLabel setFrame:CGRectMake(90, 5*Radio, Screen_Width - 90*2, [YhxUtility autoSizeToThatWidth:Screen_Width-90*2 fontsize:FLEXIBLE(13) content:self.dataSource[indexPath.row]])];
            UIButton *preview = [UIButton buttonWithType:UIButtonTypeSystem];
            preview.frame = CGRectMake(Screen_Width-80, cell.nameLabel.frame.origin.y, 70, 15*Radio);
            preview.backgroundColor = [UIColor clearColor];
            [preview setTitleColor:RGB(231, 90, 90, 1) forState:UIControlStateNormal];
            [preview setTitle:@"查看原文" forState:UIControlStateNormal];
            preview.titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
            [preview addTarget:self action:@selector(previewCurrentText) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:preview];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{
        
        UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
        
        UIView *dashedView = [[UIView alloc] initWithFrame:CGRectMake(15, 5*Radio, Screen_Width-30, 2)];
        dashedView.backgroundColor = [UIColor whiteColor];
        [cell addSubview:dashedView];
        [YhxUtility drawDashLine:dashedView lineLength:2 lineSpacing:1 lineColor:RGB(246, 246, 246, 1)];
        
        UILabel *memo = [[UILabel alloc]initWithFrame:CGRectMake(15, 20*Radio,Screen_Width-30, [YhxUtility autoSizeToThatWidth:Screen_Width-30 fontsize:FLEXIBLE(13) content:@"任务描述"])];
        memo.numberOfLines = 0;
        memo.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        memo.textColor = RGB(160, 160, 160, 1);
        memo.textAlignment = NSTextAlignmentLeft;
        memo.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
        memo.text = @"任务描述";
        [cell addSubview:memo];
        
        UILabel *Remark = [[UILabel alloc]initWithFrame:CGRectMake(15, memo.frame.origin.y+memo.frame.size.height+5*Radio,Screen_Width-30, [YhxUtility autoSizeToThatWidth:Screen_Width-30 fontsize:FLEXIBLE(14) content:self.dataSource[indexPath.row]])];
        Remark.numberOfLines = 0;
        Remark.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        Remark.textColor = RGB(106, 106, 106, 1);
        Remark.textAlignment = NSTextAlignmentLeft;
        Remark.font = [UIFont systemFontOfSize:FLEXIBLE(13)];
        Remark.text = self.dataSource[indexPath.row];
        [cell addSubview:Remark];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
}


- (void)dropOrderResponds{
    
    [self.dropBt removeFromSuperview];
    [LSProgressHUD showLoadingToView:self.view];
    [self.view endEditing:YES];

    if (orderSingleton.segementIndex == 0) {
        NSDictionary *dic = @{@"sectorId"   :self.sectorId,
                              @"ownerId"    :self.ownerId,
                              @"taskId"     :self.taskId,
                              @"vmId"       :self.vmId,
                              @"projectName":self.projectName,
                              @"taskName"   :self.taskName};
        [AvailableNetWorking postWithUrl:[YhxUtility replaceTypePath:dropOrder_connector] params:dic successHandler:^(id responseObject) {
            
            if ([responseObject[@"code"] intValue] == 200) {
                
                [LSProgressHUD hiddenLoading:self.view];
                _isSuccess = YES;
                shareData.judeHistory_refresh = YES;
                shareData.haveImage = YES;
                [JeemaaNotificationCenter postNotificationName:@"refreshOrderList" object:nil];
                [TopAlertView SetUpbackgroundColor:[UIColor whiteColor] andStayTime:2.0 andImageName:@"抢单成功" andTitleStr:@"抢单成功" andTitleColor:[UIColor blackColor] andCancel:NO];
                shareData.haveImage = NO;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
            else if ([responseObject[@"code"] intValue] == 401){
                
                [LSProgressHUD hiddenLoading:self.view];
                _isSuccess = NO;
                shareData.judeHistory_refresh = YES;
                [JeemaaNotificationCenter postNotificationName:@"refreshOrderList" object:nil];
                [TopAlertView SetUpbackgroundColor:AlertRedColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"慢了一步，下次早点来哦~" andTitleColor:[UIColor whiteColor] andCancel:NO];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
            else{
                [LSProgressHUD hiddenLoading:self.view];
                [TopAlertView SetUpbackgroundColor:AlertRedColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:responseObject[@"message"] andTitleColor:[UIColor whiteColor] andCancel:NO];
                [self.view addSubview:self.dropBt];
            }
            
        } falureHandler:^(NSError *error) {
            
            [LSProgressHUD hiddenLoading:self.view];
            [self.view addSubview:self.dropBt];
            
        }];
        
    }else{//来自平台
        
        TGLog(@"dic == %@ %@ %@",self.plattask_id,self.vmId,self.taskName);
        NSDictionary *dic = @{@"plattask_id":self.plattask_id,
                              @"vm_Id"       :self.vmId,
                              @"taskName"   :self.taskName};
        [AvailableNetWorking postWithUrl:[YhxUtility replaceTypePath:platDropOrder_connector] params:dic successHandler:^(id responseObject) {
            
            if ([responseObject[@"code"] intValue] == 200) {
                
                [LSProgressHUD hiddenLoading:self.view];
                _isSuccess = YES;
                shareData.judeHistory_refresh = YES;
                shareData.haveImage = YES;
                [JeemaaNotificationCenter postNotificationName:@"refreshOrderList" object:nil];
                [TopAlertView SetUpbackgroundColor:[UIColor whiteColor] andStayTime:2.0 andImageName:@"抢单成功" andTitleStr:@"恭喜，抢单成功！" andTitleColor:[UIColor blackColor] andCancel:NO];
                shareData.haveImage = NO;
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }
            else if ([responseObject[@"code"] intValue] == 401){
                
                [LSProgressHUD hiddenLoading:self.view];
                _isSuccess = NO;
                shareData.judeHistory_refresh = YES;
                [JeemaaNotificationCenter postNotificationName:@"refreshOrderList" object:nil];
                [TopAlertView SetUpbackgroundColor:AlertRedColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:@"慢了一步，下次早点来哦~" andTitleColor:[UIColor whiteColor] andCancel:NO];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    
                    [self.navigationController popViewControllerAnimated:YES];
                });
                
            }
            else{
                [LSProgressHUD hiddenLoading:self.view];
                [TopAlertView SetUpbackgroundColor:AlertRedColor andStayTime:2.0 andImageName:@"imagePlaceholder" andTitleStr:responseObject[@"message"] andTitleColor:[UIColor whiteColor] andCancel:NO];
                [self.view addSubview:self.dropBt];
            }
            
        } falureHandler:^(NSError *error) {
            
            [LSProgressHUD hiddenLoading:self.view];
            [self.view addSubview:self.dropBt];

        }];
        
    }
    
}

- (void)previewCurrentText{
    
    TGLog(@"预览");
    self.hidesBottomBarWhenPushed = YES;
    PreviewViewController *VC = [[PreviewViewController alloc]init];
    if (orderSingleton.segementIndex == 0) {
        VC.sectorId = self.sectorId;
    }else{
        VC.plattask_id = self.plattask_id;
        VC.segmentIndex = 0;
    }
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma getter


- (UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64) style:UITableViewStylePlain];
        _tableView.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorColor = [UIColor clearColor];
        if (Screen_Width == 320) {
            _tableView.scrollEnabled = YES;
            _tableView.bounces = YES;
        }
        else{
            _tableView.scrollEnabled = YES;
            _tableView.bounces = YES;
        }
    }
    return _tableView;
}

- (UIButton *)dropBt{
    
    if (!_dropBt) {
        _dropBt = [UIButton buttonWithType:UIButtonTypeSystem];
        _dropBt.frame = CGRectMake(20, Screen_Height - 70*Radio*2, Screen_Width-40, 45*Radio);
        _dropBt.backgroundColor = RGB(23, 135, 251, 1);
        [_dropBt setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_dropBt setTitle:@"立即抢单" forState:UIControlStateNormal];
        _dropBt.titleLabel.font = [UIFont systemFontOfSize:FLEXIBLE(16)];
        _dropBt.layer.masksToBounds = NO;
        _dropBt.layer.cornerRadius = 45*Radio/2;
        _dropBt.layer.shadowColor = RGB(23, 135, 251, 1).CGColor;
        _dropBt.layer.shadowOpacity = 0.5;
        _dropBt.layer.shadowOffset = CGSizeMake(2, 6);
        [_dropBt addTarget:self action:@selector(dropOrderResponds) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _dropBt;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end

